<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2024/1/5
 * Time: 14:51
 */

namespace app\api\command\spider;
use app\api\command\spider\SpiderBase;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\config;
use think\Db;
use QL\QueryList;

class Kanpula extends  SpiderBase
{
    private $baseDomain;
    private $ql;
    protected function configure()
    {
        $this->setName('kanpula')              		//定义命令的名字
        ->setDescription('kanpula')      //定义命令的描述
        ->addArgument('method');                   //参数1
    }

    //    php think kanpula spider
    protected function execute(Input $input, Output $output)
    {
        $this->baseDomain = "https://www.kanpula.com/jitapu/";
        require EXTEND_PATH.'phpQuery/phpQuery.php';
        $this->ql = QueryList::getInstance();
        ini_set('max_execution_time', 600);//秒为单位，自己根据需要定义
        $method = $input->getArgument('method');
        if($method == 'test'){
            $this->test();
        }

    }


    private function old_pic(){

    }

    private function test(){
        for($i=1;$i<5465;$i++){
            sleep(5);
            $url = 'https://www.kanpula.com/jitapu/index_'.$i.'.html';
            echo $url."\r\n\r\n\r\n";
            $this->list($url);
        }
    }

    private function list($url){
//        $url = "https://www.kanpula.com/jitapu/index.html";
        $html = $this->_get_url($url);
        $rules = [
//            'name' => ['h2>a','text'],
            'url'=>['h2>a','href'],
            'image'=>['.img>a>.img-cover','src'],

        ];
        $range = '#imgbox .wapost';
//        $data = QueryList::get($url)->rules($rules)->query()->getData();
        $data = $this->ql->html($html)->rules($rules)->range($range)->query()->getData()->all();
        if(!empty($data)){
            foreach($data as $k=>$v){
                $temp_old_pic = str_replace('https','http',$v['image']);
                $old_pic = $this->_mwDownImage($temp_old_pic,'old_pic');
//                echo $old_pic;exit;
//                sleep(5);
//                $this->details($v['url'],$old_pic);
            }
        }
    }

    private function details($url,$old_pic){
//        $url = "https://www.kanpula.com/jitapu/452803.html";
        if($this->_checkSpider($url)){
            $html = $this->_get_url($url);
            $yueqi = '吉他谱';
            $oldTitle = $this->ql->html($html)->find('h1')->text();
            $oldTitleArr = explode($yueqi,$oldTitle);
            $title = '';
            if(isset($oldTitleArr[0])){
                $title = $this->myTrim($oldTitleArr[0]);
            }
//        print_r($title);exit;
//        echo $title;exit;
            $data = $this->ql->html($html)->find('.logcon')->html();
            $contentArr = $this->ql->html($data)->find('p')->htmls();
            $content = [];
            if(!empty($contentArr)){
                foreach($contentArr as $k=>$v){
                    $content[] = $v;
                    if(strpos($v,'<i class="fa fa-tags">') !== false){
                        break;
                    }
                }
            }
            $contentStr = "";
            $images = '';
            if(!empty($content)){
//            $tag = end($content);
                $tags = $this->ql->html(end($content))->find('a')->texts();
                $tag = '';
                if(!empty($tags)){
                    foreach($tags as $k=>$v){
                        if(!empty($v)){
                            $tag .= $this->myTrim($v).",";
                        }
                    }
                }
                if(!empty($tag)){
                    $tag = trim($tag,',');
                }
                array_pop($content);
                foreach($content as $k=>$v){

                    if(strpos($v,'<img') !== false){
                        $src = $this->ql->html($v)->find('img')->attr('src');
                        $imageUrl = "http://www.kanpula.com".$src;
//                    echo $imageUrl."\r\n";
//                    exit;
                        $image = $this->_mwDownImage($imageUrl,'jita/'.date('Ymd'));
                        $images .= $image.',';
                        $contentStr .='<p class="img" alt="'.$title.'_'.$yueqi.'_'.Config::get('web_name').'"><img src="'.$image.'"></p>';
                    } else {
                        $contentStr .='<p>'.$v.'</p>';
                    }
                }
            }
            if(!empty($images)){
                $images = trim($images,',');
            }
            if(!empty($contentStr)){
                $contentStr = str_replace(['看谱啦'],[Config::get('web_name')],$contentStr);
            }
            $model = Model('app\common\model\Spider');
            $data = [
                'title'=>$title,
                'old_title' => $oldTitle,
                'content' => $contentStr,
                'source_url' => $url,
                'image' => $old_pic,
                'images' => $images,
                'type' => 1,
                'tag' => $tag,
            ];
//                print_r($data);exit;
            $this->insert($data);
        } else {
            echo $url."抓取过\r\n";
        }


    }
    private function insert($data){
        $articleModel = Model('app\common\model\Spider');
        $articleModel->insertGetId($data);
        echo $articleModel->getLastSql()."\r\n";

    }
    private function _checkSpider($url){
        $articleModel = Model('app\common\model\Spider');
        // var_dump($articleModel);exit;
        $articleData = $articleModel->where(['source_url'=>$url])->find();
        if(empty($articleData)){
            return true;
        } else {
            return false;
        }
        // var_dump($articleData);
    }
    private function _mwDownImage($image,$dir){
        $defaultArr = [
            'https://static2.tvzhe.com/images/style/obj_def.jpg'
        ];
        if(in_array($image,$defaultArr)){
            return "";
        }
        return $this->_downImage($image,$dir);
    }
}